package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "C端购物车相关接口")
@Slf4j
public class ShoppingCartController {
	@Autowired
	ShoppingCartService shoppingCartService;


	/**
	 * 添加购物车
	 * @param shoppingCartDTO
	 * @return
	 */
	@PostMapping("/add")
	@ApiOperation("添加购物车接口")
	public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
		log.info("添加购物车:{}", shoppingCartDTO);
		shoppingCartService.addshoppingCart(shoppingCartDTO);
		return Result.success();
	}

	/**
	 * 查看购物车
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation("获取购物车消息接口")
	public Result<List<ShoppingCart>> list() {
		log.info("获取购物车消息");
		List<ShoppingCart> shoppingCarts=shoppingCartService.list();
		return Result.success(shoppingCarts);
	}

	/**
	 * 清空购物车
	 * @return
	 */
	@DeleteMapping("/clean")
	@ApiOperation("清空购物车接口")
	public Result clean() {
		log.info("清空购物车");
		shoppingCartService.clean();
		return Result.success();
	}

	@PostMapping("/sub")
	@ApiOperation("购物车删除一个商品接口")
	public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
		log.info("购物车删除一个商品:{}", shoppingCartDTO);
		shoppingCartService.sub(shoppingCartDTO);
		return Result.success();
	}
}
